package com.food.eat.config;


import com.food.eat.mapper.FlashOrderMapper;
import com.food.eat.mapper.OrderProductMapper;
import com.food.eat.pojo.FlashOrder;
import com.food.eat.pojo.OrderProduct;
import org.n3r.idworker.Sid;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Random;

@Component
@RabbitListener(queues = "promotion.order")
public class PromotionMonitor {


    @Resource
    private Sid sid;

    @Resource
    private FlashOrderMapper flashOrderMapper;

    @Resource
    private OrderProductMapper productMapper;

    @RabbitHandler
    @Transactional
    public void receive(String msg){

        String[] split = msg.split(",");

        String userId = split[0];
        String productId = split[1];
        //1:数据组装，生成秒杀订单单据落库

        FlashOrder flashOrder = new FlashOrder();
        flashOrder.setFlashId(sid.nextShort());
        flashOrder.setUserId(Integer.valueOf(userId));
        flashOrder.setProductId(productId);
        flashOrder.setCreateOpereatorId(87);
        flashOrderMapper.insert(flashOrder);
        //2:根据商品ID,进行扣除缓存
        productMapper.dec(productId);


    }



}
